const Base = require('./base.js');
const speakeasy = require("speakeasy");
var QRCode = require('qrcode');
const user = {
  two_factor_temp_secret:'GUUXGS2PJVTDSOJDKRCEGZ2MIBIEA33ENFFF4R3FOVIS44KKHJ3Q'
}
module.exports = class extends Base {
  async verifyingAction() {
    // Let's say the user says that the token they have is 132890
    var userToken = this.get('code');
    console.log('user--',user)
    // Let's say we stored the user's temporary secret in a user object like above:
    // (This is specific to your implementation)
    var base32secret = user.two_factor_temp_secret;

    // Use verify() to check the token against the secret
    var verified = speakeasy.totp.verify({
      secret: base32secret,
      encoding: 'base32',
      token: userToken
    });

    this.success(verified)
  }
  async indexAction() {
    const secret = speakeasy.generateSecret({
      name:'test'
    });
    // var url = speakeasy.otpauthURL({ secret: secret.base32, label: 'test', algorithm: 'sha512' });
    // console.log('secret=',  secret.otpauth_url)
    // console.log('url=',  url)
    // Get the data URL of the authenticator URL
    // user.two_factor_temp_secret = secret.base32;
    // console.log('secret', secret)
    let data_url = await QRCode.toDataURL(secret.otpauth_url);
    console.log(secret)
    this.body = '<img src="' + data_url + '">';
    // return this.display();
  }
};
